package TeacherEvaluation.com.service;

import java.util.HashMap;
import java.util.List;

import javax.xml.ws.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import TeacherEvaluation.com.dao.studentDao;
import TeacherEvaluation.com.entity.PageBean;
import TeacherEvaluation.com.entity.Student;
/**
 * 学生服务类
 * @author 陈龙
 *
 */
@Service
public class studentService {
	@Autowired
	studentDao studentDao;
	//插入学生记录
	public int insertStu(Student student) {
		//查看学生学号是否存在，如果存在返回0，如果存在插入学生信息
		int i = studentDao.selectStuByNumber(student.getStu_number());
		if(i>0) {
			return 0;
		}else {
			return studentDao.insertStu(student);
		}
	};
	//查询所有学生
	public List<Student> selectStu(){
		return studentDao.selectStu();
	};
	public Student selectStuByOne(String stu_number) {
		return studentDao.selectStuByOne(stu_number);
	};
	
	//修改学生信息
	public int updateStu(Student student) {
		return studentDao.updateStu(student);
	};
	
	//修改密码
	public int updatePas(String stu_number,String stu_name2) {
		return studentDao.updatePas(stu_number,stu_name2);
	}
	//删除学生
	public int deleStu(int id) {
		int i = studentDao.deleStu(id);
		if(i==0) {
			return 0;
		}else {
			return i;
		}
	};
	
	//查询学生总数
	public int selectCount() {
		int i=studentDao.selectCount();
		return i;
	}
	
	public PageBean<Student> findByPage(int currentPage) {
		HashMap<String,Object> map = new HashMap<String,Object>();
		PageBean<Student> pageBean = new PageBean<Student>();
		
	    //封装当前页数
        pageBean.setCurrPage(currentPage);
        
		//每页显示的数据
		int pageSize=3;
		pageBean.setPageSize(pageSize);
		
		//封装总记录数
		int totalCount = studentDao.selectCount();
		pageBean.setTotalCount(totalCount);
		
		//封装总页数
		double tc = totalCount;
        Double num =Math.ceil(tc/pageSize);//向上取整
        pageBean.setTotalPage(num.intValue());
      
		map.put("start",(currentPage-1)*pageSize);
		map.put("size", pageBean.getPageSize());
		//封装每页显示的数据
		List<Student> lists = studentDao.findByPage(map);
		pageBean.setLists(lists);
		
		return pageBean;
	}

	
	
	
}
